package com.byzk.p2p.admin.base.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSON;
import com.byzk.p2p.admin.base.vo.AuthResult;

/**
 * 
 * @author suker
 *
 */
public class IDCardAuth {
	
	private static String  apix_key="6452ec792207467b565a5e9540b8f749";
	
	private static String http_url="http://v.apix.cn/apixcredit/idcardauth/idcardauth";
	
	/**
	 * 验证身份账号吗
	 * @param name  姓名
	 * @param no    身份证号码
	 * @return
	 */
	public static AuthResult validate(String name,String no) {
	    BufferedReader reader = null;
	    String result = null;
	    StringBuffer sbf = new StringBuffer();
	    http_url = http_url + "?name=" + name+"&cardno="+no;

	    try {
	        URL url = new URL(http_url);
	        HttpURLConnection connection = (HttpURLConnection) url
	                .openConnection();
	        connection.setRequestMethod("GET");
			
	        // 填入apix-key到HTTP header
	        connection.setRequestProperty("apix-key", apix_key);
	        connection.connect();
	        InputStream is = connection.getInputStream();
	        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	        String strRead = null;
	        while ((strRead = reader.readLine()) != null) {
	            sbf.append(strRead);
	            sbf.append("\r\n");
	        }
	        reader.close();
	        result = sbf.toString();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
		JSON json=(JSON) JSON.parse(result);
		AuthResult auth=JSON.toJavaObject(json,AuthResult.class);
		return auth;
	}
	
	public static void main(String[] args) {
		System.out.println(validate("吴思玲","421023199409175266").getMsg());
	}
}

